package com.factory.code.controller;


import com.factory.code.controller.param.TemplatePageParam;
import com.factory.code.controller.param.TemplateVerifyParam;
import com.factory.code.controller.vo.PageVO;
import com.factory.code.controller.vo.R;
import com.factory.code.repository.ent.TemplateENT;
import com.factory.code.service.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("template")
@Api(tags = "模板管理")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @PostMapping("/listAll")
    public R<Object> listAll() {
        List<TemplateENT> projectEnts = templateService.listAll();
        return R.ok(projectEnts);
    }

    @ApiOperation(value = "根据项目id查询所有模板")
    @PostMapping("/list/{projectId}")
    public R<Object> list(@PathVariable("projectId") int projectId) {
        List<TemplateENT> projectEnts = templateService.listAll(projectId);
        return R.ok(projectEnts);
    }

    @ApiOperation(value = "分页查询项目")
    @PostMapping("/getPage")
    public R<PageVO> list(@RequestBody TemplatePageParam param) {
        PageVO<TemplateENT> page = templateService.getPage(param);
        return R.ok(page);
    }

    @ApiOperation(value = "根据id查询模板")
    @PostMapping("/get/{id}")
    public R get(@PathVariable("id") int id) {
        return R.ok(templateService.getById(id));
    }

    @ApiOperation(value = "添加模板")
    @PostMapping("/add")
    public R add(@RequestBody TemplateENT templateConfig) {
        return templateService.add(templateConfig);
    }

    @ApiOperation(value = "更新模板")
    @PostMapping("/update")
    public R update(@RequestBody TemplateENT templateConfig) {
        return templateService.update(templateConfig);
    }

    @ApiOperation(value = "删除模板")
    @PostMapping("/del")
    public R delete(@RequestBody TemplateENT templateConfig) {
        return templateService.delete(templateConfig);
    }

    @ApiOperation(value = "验证模板内容")

    @PostMapping("/verify")
    public R<Object> verify(@RequestBody TemplateVerifyParam param) {
        String content = templateService.verifyTemplateContent(param);
        return R.ok(content);
    }

    @ApiOperation(value = "模板 Velocity变量 语法内容")
    @PostMapping("/velocityTemplate")
    public R velocityTemplate(@RequestBody TemplateVerifyParam param) {
        Integer projectId = 1;
        if (param.getProjectId() != null) {
            projectId = param.getProjectId();
        }
        return R.ok(templateService.velocityTemplate(projectId));
    }


}
